﻿using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SObject.Test
{
    [TestFixture]
    class Object_Name
    {
        class Constant
        {
            public const int Level1ObjectCount = 3;
            public const int Level2ObjectCount = 6;
            public const int Level2ObjectPerLevel1Object = Level2ObjectCount / Level1ObjectCount;
        }

        [SetUp]
        public void Setup()
        {
            level0Object = new SObject.Object("level0");

            level2Objects = new List<SObject.Object>();
            for (int i = 0; i < Constant.Level2ObjectCount; ++i)
            {
                SObject.Object localObject = new SObject.Object("level2_" + i.ToString());
                level2Objects.Add(localObject);
            }
            Assert.AreEqual(Constant.Level2ObjectCount, level2Objects.Count);

            int remainingLevel2ObjectCount = level2Objects.Count;
            level1Objects = new List<SObject.Object>();
            for ( int i = 0; i < Constant.Level1ObjectCount; ++i )
            {
                SObject.Object localObject = new SObject.Object("level1_" + i.ToString());
                level1Objects.Add(localObject);

                for (int j = 0; j < Constant.Level2ObjectPerLevel1Object; ++j)
                {
                    if (0 != remainingLevel2ObjectCount)
                    {
                        localObject.AddChild(level2Objects[--remainingLevel2ObjectCount]);
                    }
                }

                level0Object.AddChild(localObject);
            }
            Assert.AreEqual(Constant.Level1ObjectCount, level0Object.ChildrenCount);
        }

        [Test]
        [TestCase("level0_NewName")]
        public void SetName(string Name)
        {
            level0Object.Name = Name;
            VerifyChildrenCompleteName(level0Object);
        }

        private void VerifyChildrenCompleteName(SObject.Object Parent)
        {
            for (int index = 0; index < Parent.ChildrenCount; ++index)
            {
                SObject.Object child = Parent.GetChildren()[index];
                string expectedCompleteName = SObject.Object.NameUtility.ConstructCompleteName(Parent.CompleteName, child.Name);
                Assert.AreEqual(expectedCompleteName, child.CompleteName);
                VerifyChildrenCompleteName(child);
            }
        }

        private SObject.Object level0Object;
        private List<SObject.Object> level1Objects;
        private List<SObject.Object> level2Objects;
    }
}
